local null_ls = require('null-ls')

-- local code_actions = null_ls.builtins.code_actions
local formatting = null_ls.builtins.formatting

null_ls.setup({
	sources = {
		-- Python code formatter
		formatting.black,
		-- C like code formatter
		formatting.clang_format,
		-- listfiles formatter
		formatting.cmake_format,
		-- Fix common typos
		formatting.codespell.with({filetypes = {'markdown'}}),
		-- Replace whitespaces, Dart guidelines
		formatting.dart_format,
		-- Go formatter
		formatting.gofmt,
		-- Python utility/library sort
		formatting.isort,
		-- Lua formatter, slow
		-- formatting.lua_format.with({
		-- 	extra_args = {
		-- 		'--no-keep-simple-function-one-line', '--no-break-after-operator', '--column-limit=100',
		-- 		'--break-after-table-lb', '--indent-width=2'
		-- 	}
		-- }),
		-- Another lua formatter
		formatting.stylua,
		-- Multple filetypes
		formatting.prettier.with({
			filetypes = { 'html', 'json', 'yaml', 'markdown' },
		}),
		-- Shell/Bash formatter
		formatting.shfmt,
	},
	on_attach = function(client)
		if client.resolved_capabilities.document_formatting then
			vim.cmd("autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_seq_sync()")
			if client.resolved_capabilities.document_highlight then
				vim.api.nvim_exec([[
			augroup document_highlight
				autocmd! * <buffer>
				autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
				autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
				autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync()
			augroup END
		]], false)
		  end
		end
	end
})
